#!/bin/env python
#coding=utf-8

import sys
import os
from datetime import datetime, date, timedelta

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from config.tdm_config import *
import config.tdm_mongo_modules as tmm
reload(sys)
sys.setdefaultencoding('utf8')

def aggregate_test1():
    tmm.db_connect(tdm_mongo_acard_db)
    city = tmm.City.objects(name='天津').first()
    for venue in city.venues:
        if venue.disabled:
            continue
        pipeline = [
                {"$match": {"venue": venue.id, "log_tm":{"$gt": datetime(2015,7,1), "$lt": datetime(2015,8,1)}}},
                {"$group": {"_id": "$status", "total": {"$sum": 1}}}
                ]
        ret = tmm.AcardCtmLog._get_collection().aggregate(pipeline)
        # {u'ok': 1.0, u'result': [{u'total': 1342, u'_id': False}, {u'total': 575, u'_id': True}]}
        ret_true = ret_false = 0
        for ret_dict in ret.get('result'):
            if ret_dict.get('_id') == False:
                ret_false = ret_dict.get('total')
            if ret_dict.get('_id') == True:
                ret_true = ret_dict.get('total')
        print city.name, venue.name, 'ret_true:', ret_true, 'ret_false:', ret_false
    #print ret

def aggregate_test2():
    tmm.db_connect(tdm_mongo_acard_db)
    start_date = datetime(2015, 11, 1)
    end_date = datetime(2015, 11, 30)
    venue_ctm_percent_list = []
    for city in tmm.City.objects():
        for venue in city.venues:
            if venue.disabled:
                continue
            pipeline = [
                    {"$match": {"venue": venue.id, "log_tm":{"$gt": start_date, "$lt": end_date}}},
                    {"$group": {"_id": "$status", "total": {"$sum": 1}}}
                    ]
            ret = tmm.AcardCtmLog._get_collection().aggregate(pipeline)
            # {u'ok': 1.0, u'result': [{u'total': 1342, u'_id': False}, {u'total': 575, u'_id': True}]}
            print venue.name, ret.get('result')
            ret_true = ret_false = 0
            for ret_dict in ret.get('result'):
                if ret_dict.get('_id') == False:
                    ret_false = ret_dict.get('total')
                if ret_dict.get('_id') == True:
                    ret_true = ret_dict.get('total')
            percent = float(ret_true) / (ret_false + ret_true)
            ctm_perc = [venue.name, percent]
            venue_ctm_percent_list.append(ctm_perc)
            print city.name, venue.name, percent   #, 'ret_true:', ret_true, 'ret_false:', ret_false
        #print venue_ctm_percent_list
    #print ret

def aggregate_test3():
    tmm.db_connect(tdm_mongo_acard_db)
    start_date = datetime(2015, 11, 1)
    end_date = datetime(2015, 11, 30)
    venue_ctm_percent_list = []
    for city in tmm.City.objects():
        pipeline = [
                {"$match": {"city": city.id, "log_tm":{"$gt": start_date, "$lt": end_date}}},
                {"$group": {"_id": "$status", "total": {"$sum": 1}}}
                ]
        ret = tmm.AcardCtmLog._get_collection().aggregate(pipeline)
        # {u'ok': 1.0, u'result': [{u'total': 1342, u'_id': False}, {u'total': 575, u'_id': True}]}
        #print city.name, ret.get('result')
        ret_true = ret_false = 0
        for ret_dict in ret.get('result'):
            if ret_dict.get('_id') == False:
                ret_false = ret_dict.get('total')
            if ret_dict.get('_id') == True:
                ret_true = ret_dict.get('total')
        if (ret_false + ret_true):
            percent = float(ret_true) / (ret_false + ret_true)
            ctm_perc = [city.name, percent]
            venue_ctm_percent_list.append(ctm_perc)
            print city.name, percent   #, 'ret_true:', ret_true, 'ret_false:', ret_false
        #print venue_ctm_percent_list
    #print ret


if __name__ == '__main__':
    aggregate_test3()
